from lib.live.config import TencentCloud
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.live.v20180801 import live_client
from apps.student.share import app_logger
from tencentcloud.trtc.v20190722 import trtc_client


class AbstractLive(object):

    def __init__(self):
        self.SecretId = TencentCloud.SecretId
        self.SecretKey = TencentCloud.SecretKey
        self.endpoint = TencentCloud.Endpoint,
        self.PushKey = TencentCloud.PushKey
        self.PlayKey = TencentCloud.PlayKey
        self.AppId = TencentCloud.AppId
        self.liv_url = "live.tencentcloudapi.com"
        self.trtc_url = "trtc.tencentcloudapi.com"
        self.push_url = "push.momself.cn"
        self.play_url = "play.momself.cn"

    def _method(self):
        try:
            cred = credential.Credential(self.SecretId, self.SecretKey)
            httpProfile = HttpProfile()
            httpProfile.endpoint = self.liv_url
            clientProfile = ClientProfile()
            clientProfile.httpProfile = httpProfile
            client = live_client.LiveClient(cred, "ap-guangzhou", clientProfile)
            return client
        except TencentCloudSDKException as err:
            app_logger.error(err)
            return False

    def _trtc_method(self):
        try:
            cred = credential.Credential(self.SecretId, self.SecretKey)
            httpProfile = HttpProfile()
            httpProfile.endpoint = self.trtc_url
            clientProfile = ClientProfile()
            clientProfile.httpProfile = httpProfile
            client = trtc_client.TrtcClient(cred, "ap-guangzhou", clientProfile)
            return client
        except TencentCloudSDKException as err:
            app_logger.error(err)
            return False
